<?php

namespace App\Admin\Controllers\Shop;

use App\Admin\Actions\Grid\ShopOrderSupplyAction;
use App\Admin\Exports\CommonAdminCollectExport;
use App\Admin\Repositories\Shop\WxShopOrder;
use App\Admin\Actions\Grid\OrderAction;
use App\Jobs\Shop\ShopOrderDeliverInfoManage;
use App\Wen\Utils\Settings;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxShopOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $app_coin_name = Settings::get('app_coin_name', '硬币');
        $admin_user = Admin::user();

        return Grid::make(new WxShopOrder(['user']), function (Grid $grid) use ($app_coin_name, $admin_user){
            if(!$admin_user->isAdministrator()){
                if(_empty_($admin_user->uid)){
                    $grid->model()->where('id', '<', -1);
                }else{
                    $grid->model()->where('seller_user_id', $admin_user->uid);
                }
            }
            $grid->simplePaginate();
            $grid->quickSearch(['id', 'order_id', 'goods.name', 'user_id'])->placeholder('搜索记录id，订单ID，商品名称，用户id...')->width(70);
            $grid->model()->where('order_from', 0)->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('order_id');
            $grid->column('user', '用户')->display(function ($v){
                if(_empty_($v)){
                    return '';
                }
                return '<a href="'.admin_url('/users?id='.$v->id).'" target="_blank"><img src="'.$v->user_avatar.'" style="width: 30px; height: 30px;margin-right: 10px; border-radius: 50%;">'.$v->user_name. '</a>';
            });
            $grid->column('order_googs', '购物袋商品')->display(function (){
                return '<a href="'.admin_url('/shop/orderGoods?id=&order_id='.$this->id).'" target="_blank">查看商品</a>';
            });
            $grid->column('seller', '商家')->display(function ($v){
                if(_empty_($v)){
                    return '系统自营';
                }
                return '<a href="'.admin_url('/users?id='.$v->id).'" target="_blank"><img src="'.$v->user_avatar.'" style="width: 30px; height: 30px;margin-right: 10px; border-radius: 50%;">'.$v->user_name. '</a>';
            });
            $grid->column('goods_amount_total', '商品总价')->display(function ($v) use ($app_coin_name){
                $str = '';
                if($this->goods_amount > 0){
                    $str .= '￥'.$this->goods_amount;
                }
                if($this->goods_amount > 0 && $this->goods_amount_coin > 0){
                    $str .= '+';
                }
                if($this->goods_amount_coin > 0){
                    $str .= $app_coin_name.$this->goods_amount_coin;
                }
                return $str;
            });
            $grid->column('discounts_amount_total', '优惠金额')->display(function ($v) use ($app_coin_name){
                $str = '';
                if($this->discounts_amount > 0){
                    $str .= '￥'.$this->discounts_amount;
                }
                if($this->discounts_amount > 0 && $this->discounts_amount_coin > 0){
                    $str .= '+';
                }
                if($this->discounts_amount_coin > 0){
                    $str .= $app_coin_name.$this->discounts_amount_coin;
                }
                return $str;
            });
            $grid->column('order_amount_total', '订单总额')->display(function ($v) use ($app_coin_name){

                if($this->status == 9){
                    ShopOrderDeliverInfoManage::dispatch('virtual-upload', $this->id, null);
                }else if($this->status == 10){
                    ShopOrderDeliverInfoManage::dispatch('virtual-query', $this->id, null);
                }

                $str = '';
                if($this->order_amount > 0){
                    $str .= '￥'.$this->order_amount;
                }
                if($this->order_amount > 0 && $this->order_amount_coin > 0){
                    $str .= '+';
                }
                if($this->order_amount_coin > 0){
                    $str .= $app_coin_name.$this->order_amount_coin;
                }
                return $str;
            });
            $grid->column('payed', '支付金额');
            $grid->column('pay_way', '支付方式');
//            $grid->column('serial_number');
            $grid->column('pay_status')->using([1 => '未付款', 2 => '已付款', 3 => '已退款'])->label([
                1 => 'gray',
                2 => 'green',
                3 => 'red'
            ]);
            $grid->column('status', '状态')->using([0 => '正常', 1 => '未发货', 2 => '已发货', 3 => '确认收货', 4 => '已退货', 5 => '取消', 6 => '用户删除', 7=>'退款中', 8=>'自提发货待确定', 9=>'虚拟发货待同步', 10=>'虚拟发货待确定'])->label([
                0 => 'blue',
                1 => 'red',
                2 => 'primary',
                3 => 'green',
                4 => 'orange2',
                5 => 'pink',
                6 => 'gray',
                7 => 'red',
                8 => 'gray'
            ]);
            $grid->column('cost_freight', '配送费');
//            $grid->column('adds_name');
//            $grid->column('adds_mobile');
//            $grid->column('address');
//            $grid->column('express');
//            $grid->column('express_no');
//            $grid->column('remark');
            $grid->column('user_remark')->limit(20);
//            $grid->column('payment_time');
//            $grid->column('ship_at');
//            $grid->column('confirm_time');
            $grid->column('created_at')->sortable();
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('order_id');
                $filter->where('order_googs', function ($query) {
                    $query->whereHas('goods', function ($query) {
                        $query->where('name', 'like', "%{$this->input}%");
                    });
                }, '包含商品名称');

                $filter->equal('user_id')->select()->ajax('select/users');
                $filter->equal('pay_status')->multipleSelect([1 => '未付款', 2 => '已付款', 3 => '已退款']);
                $filter->equal('status')->multipleSelect([0 => '正常', 1 => '未发货', 2 => '已发货', 3 => '确认收货', 4 => '已退货', 5 => '取消', 6 => '用户删除', 7=>'退款中', 8=>'自提发货待确定', 9=>'虚拟发货待同步', 10=>'虚拟发货待确定']);
                $filter->between('goods_amount');
                $filter->between('discounts_amount');
                $filter->between('order_amount');
                $filter->between('payed');
                $filter->between('created_at')->datetime();
                $filter->between('payment_time')->datetime();
                $filter->between('confirm_time')->datetime();
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new OrderAction())->setKey($this->id));
            });
            $grid->disableCreateButton();
//            $grid->disableActions();
            // 禁用编辑按钮
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->batchActions(function ($batch) {
                $batch->add(new ShopOrderSupplyAction());
            });
            $grid->export(new CommonAdminCollectExport('商品订单数据', 'WxShopOrder'));
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxShopOrder(), function (Show $show) {
            $show->field('id');
            $show->field('order_id');
            $show->field('user_id');
            $show->field('goods_amount');
            $show->field('discounts_amount');
            $show->field('order_amount');
            $show->field('payed');
            $show->field('serial_number');
            $show->field('pay_status');
            $show->field('status');
            $show->field('cost_freight');
            $show->field('adds_id');
            $show->field('adds_name');
            $show->field('adds_mobile');
            $show->field('address');
            $show->field('express');
            $show->field('express_no');
            $show->field('remark');
            $show->field('user_remark');
            $show->field('payment_time');
            $show->field('ship_at');
            $show->field('confirm_time');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxShopOrder(), function (Form $form) {
            $form->display('id');
            $form->text('order_id');
            $form->text('user_id');
            $form->text('goods_amount');
            $form->text('discounts_amount');
            $form->text('order_amount');
            $form->text('payed');
            $form->text('serial_number');
            $form->text('pay_status');
            $form->text('status');
            $form->text('cost_freight');
            $form->text('adds_id');
            $form->text('adds_name');
            $form->text('adds_mobile');
            $form->text('address');
            $form->text('express');
            $form->text('express_no');
            $form->text('remark');
            $form->text('user_remark');
            $form->text('payment_time');
            $form->text('ship_at');
            $form->text('confirm_time');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
